using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Xml;

namespace DacTaHinhThuc
{
    public partial class SettingFrm : Form
    {
        #region fields
        private Settings m_setting;     //luu thiet lap ban dau
        private Settings m_buf;         //luu thiet lap nguoi dung dang chon
        #endregion

        #region properties
        public Settings Setting
        {
            get { return this.m_setting; }
            set { this.m_setting = value; }
        }
        #endregion

        public SettingFrm(Settings s)
        {
            InitializeComponent();

            this.m_setting = s;
        }    
                
        private void SettingFrm_Load(object sender, EventArgs e)
        {            
            picColorKeyword.BackColor = GetColor(this.m_setting.ColorKeyword);
            picColorComment.BackColor = GetColor(this.m_setting.ColorComments);
            picColorStr.BackColor = GetColor(this.m_setting.ColorString);
            picColorNameFunc.BackColor = GetColor(this.m_setting.ColorNameFunc);

            this.m_buf = this.m_setting;
                        
        }

        private void UpdateColor()
        {
            //update cac mau hien thi
            picColorKeyword.BackColor = GetColor(this.m_buf.ColorKeyword);
            picColorComment.BackColor = GetColor(this.m_buf.ColorComments);
            picColorStr.BackColor = GetColor(this.m_buf.ColorString);
            picColorNameFunc.BackColor = GetColor(this.m_buf.ColorNameFunc);
        }

        /// <summary>
        /// lay mau tu chuoi
        /// </summary>
        /// <param name="s">chuoi can lay mau co dang (A,R,G,B) hoac ten duoc dinh nghia san</param>
        static public Color GetColor(string s)
        {
            try
            {
                int red;
                int green;
                int blue;
                int alpha;

                alpha = int.Parse(s.Substring(0, 2), System.Globalization.NumberStyles.HexNumber);
                red = int.Parse(s.Substring(2, 2), System.Globalization.NumberStyles.HexNumber);
                green = int.Parse(s.Substring(4, 2), System.Globalization.NumberStyles.HexNumber);
                blue = int.Parse(s.Substring(6, 2), System.Globalization.NumberStyles.HexNumber);

                return Color.FromArgb(alpha, red, green, blue);
            }
            catch
            {
                return Color.FromName(s);                
            }
        }                

        private void btnColorKeyword_Click(object sender, EventArgs e)
        {
            if (colorDialog1.ShowDialog() == DialogResult.OK)
            {
                //lay mau nguoi dung chon
                this.m_buf.ColorKeyword = colorDialog1.Color.Name;                             

                UpdateColor();
            }
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Ban co muon luu lai cac thay doi hay khong?", "Thong bao", MessageBoxButtons.OKCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1) == DialogResult.OK)
            {
                this.m_setting = this.m_buf;
                this.Close();
                this.DialogResult = DialogResult.OK;
            }            
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
            this.DialogResult = DialogResult.Cancel;
        }

        private void btnColorString_Click(object sender, EventArgs e)
        {
            if (colorDialog1.ShowDialog() == DialogResult.OK)
            {
                //lay mau nguoi dung chon
                this.m_buf.ColorString = colorDialog1.Color.Name;

                UpdateColor();
            }
        }

        private void btnColorComment_Click(object sender, EventArgs e)
        {
            if (colorDialog1.ShowDialog() == DialogResult.OK)
            {
                //lay mau nguoi dung chon
                this.m_buf.ColorComments = colorDialog1.Color.Name;

                UpdateColor();
            }
        }

        private void btnColorNameFunc_Click(object sender, EventArgs e)
        {
            if (colorDialog1.ShowDialog() == DialogResult.OK)
            {
                //lay mau nguoi dung chon
                this.m_buf.ColorNameFunc = colorDialog1.Color.Name;

                UpdateColor();
            }
        }

        
    }
}